package com.quyeying.controller;

import com.quyeying.entity.TblMerchantEntity;
import com.quyeying.service.TblMerchantService;
import com.quyeying.utils.JwtUtil;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ResultEnum;
import org.springframework.web.bind.annotation.*;
import com.quyeying.service.TblMerchantAuthenticationService;
import com.quyeying.entity.TblMerchantAuthenticationEntity;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
* <p>
* tbl_merchant_authentication 前端控制器
* </p>
* @author jll
* @since 2022-12-14
*/
@Slf4j
@RestController
@RequestMapping("merchantAuthenticationEntity")
public class TblMerchantAuthenticationController {

    @Resource
    private TblMerchantAuthenticationService tblMerchantAuthenticationService;
    @Resource
    private TblMerchantService tblMerchantService;

    /**
    * 新增
    **/
    @PostMapping("/add")
    public R add(@RequestBody Map<String, Object> map, HttpServletRequest request){
        try {
            String authorization = request.getHeader("Authorization");
            String userName = JwtUtil.getUserName(authorization);
            TblMerchantEntity merchantInfo = tblMerchantService.getMerchantInfo(userName);
            map.put("merchant_id", merchantInfo.getId());
            return tblMerchantAuthenticationService.add(map);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 删除
    **/
    @DeleteMapping("/del")
    public R delete(Long id){
        try {
            return tblMerchantAuthenticationService.delete(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 更新
    **/
    @PostMapping("/update")
    public R update(@RequestBody TblMerchantAuthenticationEntity tblMerchantAuthenticationEntity){
        try {
            return tblMerchantAuthenticationService.updateData(tblMerchantAuthenticationEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 分页查询
    **/
    @GetMapping("/findListByPage")
    public R findListByPage(@RequestParam Integer current,
    @RequestParam Integer size){
        try {
            return tblMerchantAuthenticationService.findListByPage(current, size);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * id查询
    **/
    @GetMapping("/findById")
    public R findById(Long id){
        try {
            return tblMerchantAuthenticationService.findById(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }
}
